/**************************************************************************************************
* Title: InputManager.cpp
* Authors: Gael Huber
* Description: Handles mouse and keyboard input.
**************************************************************************************************/
#include "SmurfsInputManager.h"
#include "SimulationManager.h"

// Singleton instance
template<> InputManager* Singleton<InputManager>::singleton = 0;

// Constructor
InputManager::InputManager(void) {
	time(&prevTime);
}

// Destructor
InputManager::~InputManager(void) { }

/**************************************************************************************************
* Get the singleton instance
**************************************************************************************************/
InputManager& InputManager::getSingleton(void) { 
	assert(singleton);
	return *singleton;
}

/**************************************************************************************************
* Get the singleton pointer
**************************************************************************************************/
InputManager* InputManager::getSingletonPtr(void) {
	return singleton;
}

// Public Method to access both keyboard and mouse input
void InputManager::handleInput(void) {
	handleKeyboardInput();
	handleMouseInput();
}

// Handles keyboard input
void InputManager::handleKeyboardInput(void) {
	// Find the elapsed time
	time_t cTime;
	time(&cTime);
	time_t elapsedTime = cTime - prevTime;

	if((GetKeyState(0x54) & 0x80)) {	// T key
	} else if((GetKeyState(0x41) & 0x80)) {	// A key
		if(elapsedTime >= 1) {
			// Determine a random force to apply
			float x = (float)(rand() % 2000 - 1000);
			float y = (float)(rand() % 2000 - 1000);
			float z = (float)(rand() % 2000 - 1000);

			// Compile the force
			Vector3 force = Vector3(x, y, z);

			// Apply the force to the bar
			SimulationManager::getSingletonPtr()->getBar()->getBody()->appliedForce = force;
		}
	} else if((GetKeyState(VK_SPACE) & 0x80)) {
		// If enough time has elapsed, toggle paused
		if(elapsedTime >= 1) {
			toggleGamePaused();
			prevTime = cTime;
		}
	} else if((GetKeyState(VK_ESCAPE) & 0x80)) {
		// Quit the application
		PostQuitMessage(0);
	}
}

// Handles mouse input
void InputManager::handleMouseInput(void) {
	if((GetKeyState(VK_LBUTTON) & 0x80)) {
	}
}